package service.report.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import model.TContract;
import model.app.AppFinance;
import model.app.AppInfo;

import service.report.ReportCountService;
import util.MoneyCalc;
import vo.MoneyDoubleBean;
import dao.LoangrantDao;
import dao.LoangrantrecordDao;
import dao.LoaninterestDao;
import dao.LoaninterestrecordDao;
import dao.TContractDao;
import dao.app.AppFinanceDao;
import dao.app.AppInfoDAO;
import exception.Exceptions;

public class ReportCountServiceImpl implements ReportCountService {
	private TContractDao tContractDao;
	
	private LoangrantDao loangrantDao;
	
	private LoangrantrecordDao loangrantrecordDao;
	
	private LoaninterestDao loaninterestDao;
	
	private LoaninterestrecordDao loaninterestrecordDao;
	
	private AppFinanceDao appFinanceDao;
	
	private AppInfoDAO appInfoDAO;

	private String searchDate;
	
	private BigDecimal money5;
	
	private BigDecimal money10;
	
	private BigDecimal allMoney;//总注册资本

	@Override
	public Map<String, BigDecimal> getReportCountMap(String searchDate)
			throws Exceptions {
		this.searchDate = searchDate;
		List<AppFinance> appFinanceList = this.appFinanceDao.findModel("DATE_FORMAT(financedate, '%Y-%m') = DATE_FORMAT('" + this.searchDate + "', '%Y-%m')");
		if(appFinanceList != null){
			allMoney = new BigDecimal(appFinanceList.get(0).getOperatregfinance());
		}else{
			allMoney = new BigDecimal(100000000.00);
		}
		if(allMoney.compareTo(new BigDecimal(100000000)) == -1){
			this.money5 = new BigDecimal(500000);
		}else{
			this.money5 = allMoney.multiply(new BigDecimal(0.05)).setScale(2, BigDecimal.ROUND_HALF_UP);
		}
		this.money10 = allMoney.multiply(new BigDecimal(0.10)).setScale(2, BigDecimal.ROUND_HALF_UP);
		
		
		Map<String, BigDecimal> countMap = new HashMap<String, BigDecimal>();

		// 单笔最高利率
		BigDecimal oneHigRate = this.getApplyRate(2);
		countMap.put("oneHigRate", oneHigRate);

		// 单笔最低利率
		BigDecimal oneLowRate = this.getApplyRate(1);
		countMap.put("oneLowRate", oneLowRate);

		// 当月平均利率
		BigDecimal thisMonAvgRate = this.getAvgRate(1);
		countMap.put("thisMonAvgRate", thisMonAvgRate);

		// 累计平均利率
		BigDecimal allAvgRate = this.getAvgRate(2);
		countMap.put("allAvgRate", allAvgRate);

		// 当月贷款额
		BigDecimal atmtm = this.getLoan(1);
		countMap.put("atmtm", atmtm);

		// 小计户数
		BigDecimal allNumber = this.getAllRateRecordHCount(1);
		countMap.put("allNumber", allNumber);

		// 小计笔数
		BigDecimal acbont = this.getAllRateRecordCount(1);
		countMap.put("acbont", acbont);


		// 0-6宗累计
		BigDecimal tmbtt6 = this.getMoneyByDateLine(1);
		countMap.put("tmbtt6", tmbtt6);

		// 7-12总累计
		BigDecimal tmbtt7 = this.getMoneyByDateLine(2);
		countMap.put("tmbtt7", tmbtt7);

		// >12总累计
		BigDecimal tmbtt12 = this.getMoneyByDateLine(3);
		countMap.put("tmbtt12", tmbtt12);
		
		// 小计金额
		BigDecimal atmbt = tmbtt6.add(tmbtt7).add(tmbtt12);
		countMap.put("atmbt", atmbt);

		// 涉农户数
		BigDecimal allNumberN = this.getAllRateRecordHCount(2);
		countMap.put("allNumberN", allNumberN);

		// 涉农笔数累计
		BigDecimal cbont = this.getAllRateRecordCount(2);
		countMap.put("cbont", cbont);

		// 涉农金额总累计
		BigDecimal tmbotn = this.getMoneyByObject(1);
		countMap.put("tmbotn", tmbotn);

		// 企业户数
		BigDecimal allNumberE = this.getAllRateRecordHCount(3);
		countMap.put("allNumberE", allNumberE);

		// 企业笔数累计
		BigDecimal cboet = this.getAllRateRecordCount(3);
		countMap.put("cboet", cboet);

		// 企业呢总累计
		BigDecimal tmbote = this.getMoneyByObject(2);
		countMap.put("tmbote", tmbote);

		// 其它户数
		BigDecimal allNumberP = this.getAllRateRecordHCount(4);
		countMap.put("allNumberP", allNumberP);

		// 其它（个人）笔数累计
		BigDecimal cbopt = this.getAllRateRecordCount(4);
		countMap.put("cbopt", cbopt);

		// 个人呢总累计
		BigDecimal tmbotp = this.getMoneyByObject(2);
		countMap.put("tmbotp", tmbotp);

		// 当月余额
		BigDecimal tmm = this.getBalance(1);
		countMap.put("tmm", tmm);

		// 涉农余额
		BigDecimal mbon = this.getBalanceByObject(1);
		countMap.put("mbon", mbon);

		// 企业余额
		BigDecimal mboe = this.getBalanceByObject(2);
		countMap.put("mboe", mboe);

		// 最高单户贷款余额不超过50万
		BigDecimal mbm50 = this.getBalanceByMoneyType(1, 1);
		countMap.put("mbm50", mbm50);

		// 最高单户贷款余额不超过100万
		BigDecimal mbm100 = this.getBalanceByMoneyType(2, 1);
		countMap.put("mbm100", mbm100);

		// 最高单户贷款余额
		BigDecimal hightMoneyOne = this.getHightMoney();
		countMap.put("hightMoneyOne", hightMoneyOne);

		return countMap;
	}

	// 单笔利率1 最低  2最高
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private BigDecimal getApplyRate(int type){
		List<MoneyDoubleBean> list = this.getListBean(1);
		
		if(list != null){
			Collections.sort(list, new Comparator() {  
		          public int compare(Object a, Object b) {  
		            Double one = ((MoneyDoubleBean)a).getD();
		            Double two = ((MoneyDoubleBean)b).getD();
		            if(one > two){
		        		return 1;
		        	}else if(one < two){
		        		return -1;
		        	}else{
		        		return 0;
		        	}
		          } 
		       });
		}else{
			return new BigDecimal(0.00000);
		}
		
		if(list != null && list.size() != 0){
			if(type == 1){
				return new BigDecimal(list.get(0).getD()).setScale(5, BigDecimal.ROUND_HALF_UP);
			}else if(type == 2){
				return new BigDecimal(list.get(list.size() - 1).getD()).setScale(5, BigDecimal.ROUND_HALF_UP);
			}else{
				return new BigDecimal(0.00000);
			}
		}else{
			return new BigDecimal(0.00000);
		}
	}
	
	// 贷款利率总计
		@SuppressWarnings({ "unchecked", "rawtypes" })
		protected BigDecimal getAllRateCount(int type){
			List list = this.getListBean(type);
			if(list == null){
				return new BigDecimal(0.00000);
			}
			return MoneyCalc.moneyTotal(list).setScale(5, BigDecimal.ROUND_HALF_UP);
		}
		
		// 贷款比数总计
		@SuppressWarnings({ "rawtypes"})
		protected BigDecimal getAllRateRecordCount(int type){
			List list = this.getListBean(type);
			if(list == null){
				return new BigDecimal(0);
			}
			return new BigDecimal(list.size());
		}
		
		// 本月贷款户数总计
		/**
		 * type 
		 * 1 本月
		 * 2涉农
		 * 3企业
		 * 4其它
		 * */
		@SuppressWarnings({ "rawtypes", "unchecked"})
		protected BigDecimal getAllRateRecordHCount(int type){
			List<Integer> listP = new ArrayList<Integer>();
			List<Integer> listE = new ArrayList<Integer>();
			String select = "DISTINCT l.contractid";
			String from = "";
			if(type == 1){
				from = "Loangrant as l where DATE_FORMAT(l.grantdate, '%Y-%m') = DATE_FORMAT('" + this.searchDate + "', '%Y-%m') and (l.state = 2 or l.state = 3 or l.state = 0)";
			}else if(type == 2){
				from = "Loangrant as l, t_contract as c where DATE_FORMAT(l.grantdate, '%Y-%m') = DATE_FORMAT('" + this.searchDate + "', '%Y-%m') and (l.state = 2 or l.state = 3 or l.state = 0) and c.isaboutn = 1 and (c.contractstate = 1 or c.contractstate = 0)";
			}else if(type == 3){
				from = "Loangrant as l, t_contract as c where DATE_FORMAT(l.grantdate, '%Y-%m') = DATE_FORMAT('" + this.searchDate + "', '%Y-%m') and (l.state = 2 or l.state = 3 or l.state = 0) and c.clienttype = '2' and (c.contractstate = 1 or c.contractstate = 0)";
			}else if(type == 4){
				from = "Loangrant as l, t_contract as c where DATE_FORMAT(l.grantdate, '%Y-%m') = DATE_FORMAT('" + this.searchDate + "', '%Y-%m') and (l.state = 2 or l.state = 3 or l.state = 0) and c.clienttype <> '2' and c.isaboutn <> 1 and (c.contractstate = 1 or c.contractstate = 0)";
			}
			
			List<?> listBean = this.loangrantDao.findSqlQuery(select, from, 0);
			
			if(listBean != null){
				for(int i = 0, j = listBean.size(); i < j; i++){
					Map<Object,Object> mapbean = (Map<Object, Object>)listBean.get(i);
					int loanid = (Integer)mapbean.get("contractid");
					TContract tContract = this.tContractDao.findByOne("Id = " + loanid + " and (contractstate = 1 or contractstate = 0)");
					if(tContract.getClienttype().equals("2")){
						listE.add(tContract.getClientid());
					}else if(tContract.getClienttype().equals("1")){
						listP.add(tContract.getClientid());
					}
				}
			}
			if(listE != null){
				HashSet h = new HashSet(listE);
				listE.clear();
				listE.addAll(h);
			}
			
			if(listP != null){
				HashSet h = new HashSet(listP);
				listP.clear();
				listP.addAll(h);
			}
			return new BigDecimal(((listE==null||listE.isEmpty())?0:listE.size()) + ((listP==null||listP.isEmpty())?0:listP.size()));    
		}
	
	/**
	 * type = 1 当月平均利率
	 * type = 2 累计平均利率
	 * */
	protected BigDecimal getAvgRate(int type){
		BigDecimal allRate = new BigDecimal(0);
		BigDecimal allCount = new BigDecimal(0);
			allRate = this.getAllRateCount(type);
			allCount = this.getAllRateRecordCount(type);
		try {
			return allRate.divide(allCount).setScale(5,BigDecimal.ROUND_HALF_UP);
		} catch (Exception e) {
			return new BigDecimal(0.00).setScale(5, BigDecimal.ROUND_HALF_UP);
		}
	}
	
	/**
	 * type = 1 当月
	 * type = 2 涉农
	 * type = 3 企业
	 * type = 4 其他
	 * */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private List getListBean(int type){
		List<MoneyDoubleBean> list = new ArrayList<MoneyDoubleBean>();
		String select = "DISTINCT l.contractid";
		String from = "";
		if(type == 1){
			from = "Loangrant as l where DATE_FORMAT(l.grantdate, '%Y-%m') = DATE_FORMAT('" + this.searchDate + "', '%Y-%m') and (l.state = 2 or l.state = 3 or l.state = 0)";
		}else if(type == 2){
			from = "Loangrant as l, t_contract as c where DATE_FORMAT(l.grantdate, '%Y-%m') = DATE_FORMAT('" + this.searchDate + "', '%Y-%m') and (l.state = 2 or l.state = 3 or l.state = 0) and c.isaboutn = 1 and (c.contractstate = 1 or c.contractstate = 0)";
		}else if(type == 3){
			from = "Loangrant as l, t_contract as c where DATE_FORMAT(l.grantdate, '%Y-%m') = DATE_FORMAT('" + this.searchDate + "', '%Y-%m') and (l.state = 2 or l.state = 3 or l.state = 0) and c.clienttype = '2' and (c.contractstate = 1 or c.contractstate = 0)";
		}else if(type == 4){
			from = "Loangrant as l, t_contract as c where DATE_FORMAT(l.grantdate, '%Y-%m') = DATE_FORMAT('" + this.searchDate + "', '%Y-%m') and (l.state = 2 or l.state = 3 or l.state = 0) and c.clienttype <> '2' and c.isaboutn <> 1 and (c.contractstate = 1 or c.contractstate = 0)";
		}
		
		List<?> listBean = this.loangrantDao.findSqlQuery(select, from, 0);
		
		if(listBean != null){
			for(int i = 0, j = listBean.size(); i < j; i++){
				Map<Object,Object> mapbean = (Map<Object, Object>)listBean.get(i);
				int loanid = (Integer)mapbean.get("contractid");
				TContract tContract = this.tContractDao.findByOne("Id = " + loanid + " and (contractstate = 1 or contractstate = 0)");
				list.add(new MoneyDoubleBean(tContract.getPerformrates()));
			}
		}
		return list;
	}
	
	/**
	 * 贷款额
	 * type = 1 当月
	 * type = 2 当年
	 * */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private BigDecimal getLoan(int type){
		String select = "new vo.MoneyDoubleBean(l.grantmoney)";
		String from = "";
		if(type == 1){
			from = "Loangrant as l where DATE_FORMAT(l.grantdate, '%Y-%m') = DATE_FORMAT('" + this.searchDate + "', '%Y-%m') and (l.state = 2 or l.state = 3 or l.state = 0)";
		}else if(type == 2){
			from = "Loangrant as l where DATE_FORMAT(l.grantdate, '%Y') = DATE_FORMAT('" + this.searchDate + "', '%Y') and (l.state = 2 or l.state = 3 or l.state = 0)";
		}
		
		List list = this.loaninterestDao.findQuery(select, from);
		return MoneyCalc.moneyTotal(list).divide(new BigDecimal(10000)).setScale(2, BigDecimal.ROUND_HALF_UP);
	}
	
	/**
	 * 贷金额 按照期限划分
	 * type = 1 0-6
	 * type = 2 7-12
	 * type = 3 >12
	 * */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private BigDecimal getMoneyByDateLine(int type){
		String select = "new vo.MoneyDoubleBean(l.grantmoney)";
		String from = "";
		if(type == 1){
			from = "Loangrant as l, TContract as c where TIMESTAMPDIFF(MONTH,c.startdate,c.enddate) <= 6 and TIMESTAMPDIFF(MONTH,c.startdate,c.enddate) >= 0 and DATE_FORMAT(l.grantdate, '%Y-%m') = DATE_FORMAT('" + this.searchDate + "', '%Y-%m') and (l.state = 0 or l.state = 2 or l.state = 3) and (c.contractstate = 1 or c.contractstate = 0) and l.contractid = c.id";
		}else if(type == 2){
			from = "Loangrant as l, TContract as c where TIMESTAMPDIFF(MONTH,c.startdate,c.enddate) <= 12 and TIMESTAMPDIFF(MONTH,c.startdate,c.enddate) >= 7 and DATE_FORMAT(l.grantdate, '%Y-%m') = DATE_FORMAT('" + this.searchDate + "', '%Y-%m') and (l.state = 0 or l.state = 2 or l.state = 3) and (c.contractstate = 1 or c.contractstate = 0) and l.contractid = c.id";
		}else if(type == 3){
			from = "Loangrant as l, TContract as c where TIMESTAMPDIFF(MONTH,c.startdate,c.enddate) > 12 and DATE_FORMAT(l.grantdate, '%Y-%m') = DATE_FORMAT('" + this.searchDate + "', '%Y-%m') and (l.state = 0 or l.state = 2 or l.state = 3) and (c.contractstate = 1 or c.contractstate = 0) and l.contractid = c.id";
		}
		
		List list = this.loaninterestDao.findQuery(select, from);
		return MoneyCalc.moneyTotal(list).divide(new BigDecimal(10000)).setScale(2, BigDecimal.ROUND_HALF_UP);
	}
	
	
	/**
	 * 贷款金额 按照资金投向划分
	 * type = 1 涉农
	 * type = 2 企业
	 * type = 3 其他
	 * */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private BigDecimal getMoneyByObject(int type){
		String select = "new vo.MoneyDoubleBean(l.grantmoney)";
		String from = "";
		if(type == 1){
			from = "Loangrant as l, TContract as c where c.isaboutn = 1 and DATE_FORMAT(l.grantdate, '%Y-%m') = DATE_FORMAT('" + this.searchDate + "', '%Y-%m') and (l.state = 0 or l.state = 2 or l.state = 3) and (c.contractstate = 1 or c.contractstate = 0) and l.contractid = c.id";
		}else if(type == 2){
			from = "Loangrant as l, TContract as c where c.clienttype = 2 and DATE_FORMAT(l.grantdate, '%Y-%m') = DATE_FORMAT('" + this.searchDate + "', '%Y-%m') and (l.state = 0 or l.state = 2 or l.state = 3) and (c.contractstate = 1 or c.contractstate = 0) and l.contractid = c.id";
		}else if(type == 3){
			from = "Loangrant as l, TContract as c where (c.isaboutn <> 1 and c.clienttype <> 2) and DATE_FORMAT(l.grantdate, '%Y-%m') = DATE_FORMAT('" + this.searchDate + "', '%Y-%m') and (l.state = 0 or l.state = 2 or l.state = 3) and (c.contractstate = 1 or c.contractstate = 0) and l.contractid = c.id";
		}
		
		List list = this.loaninterestDao.findQuery(select, from);
		return MoneyCalc.moneyTotal(list).divide(new BigDecimal(10000)).setScale(2, BigDecimal.ROUND_HALF_UP);
	}
	
	/**
	 * 贷款余额
	 * type = 1当月
	 * type = 2当年
	 * type = 3累计
	 * */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private BigDecimal getBalance(int type){
		String select = "new vo.MoneyDoubleBean(l.balance)";
		String from = "";
		if(type == 1){
			from = "Loangrant as l where DATE_FORMAT(l.grantdate, '%Y-%m') = DATE_FORMAT('" + this.searchDate + "', '%Y-%m') and (l.state = 2 or l.state = 3 or l.state = 0)";
		}else if(type == 2){
			from = "Loangrant as l where DATE_FORMAT(l.grantdate, '%Y') = DATE_FORMAT('" + this.searchDate + "', '%Y') and (l.state = 2 or l.state = 3 or l.state = 0)";
		}else if(type == 3){
			from = "Loangrant as l where (l.state = 2 or l.state = 3 or l.state = 0)";
		}
		
		List list = this.loaninterestDao.findQuery(select, from);
		return MoneyCalc.moneyTotal(list).divide(new BigDecimal(10000)).setScale(2, BigDecimal.ROUND_HALF_UP);
	}
	
	
	/**
	 * 贷款余额 按照资金投向划分
	 * type = 1 涉农
	 * type = 2 企业
	 * type = 3 其他
	 * */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private BigDecimal getBalanceByObject(int type){
		String select = "new vo.MoneyDoubleBean(l.balance)";
		String from = "";
		if(type == 1){
			from = "Loangrant as l, TContract as c where c.isaboutn = 1 and (l.state = 0 or l.state = 2 or l.state = 3) and (c.contractstate = 1 or c.contractstate = 0) and l.contractid = c.id";
		}else if(type == 2){
			from = "Loangrant as l, TContract as c where c.clienttype = 2 and (l.state = 0 or l.state = 2 or l.state = 3) and (c.contractstate = 1 or c.contractstate = 0) and l.contractid = c.id";
		}else if(type == 3){
			from = "Loangrant as l, TContract as c where (c.isaboutn <> 1 and c.clienttype <> 2) and (l.state = 0 or l.state = 2 or l.state = 3) and (c.contractstate = 1 or c.contractstate = 0) and l.contractid = c.id";
		}
		
		List list = this.loaninterestDao.findQuery(select, from);
		return MoneyCalc.moneyTotal(list).divide(new BigDecimal(10000)).setScale(2, BigDecimal.ROUND_HALF_UP);
	}
	
	
	/**
	 * 贷款余额 按照资金划分
	 * type = 1 <=%5
	 * type = 2 >5% & <= 10%
	 * type = 3 >10%
	 * 
	 * dType = 1 返回余额
	 * dType = 2 返回比数
	 * @throws Exceptions 
	 * */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private BigDecimal getBalanceByMoneyType(int type, int dType) throws Exceptions{
		List list = this.getBalanceByMoney(type);
		if(dType == 1){
			if(list != null){
				return new BigDecimal(list.size());
			}else{
				return new BigDecimal(0.00);
			}
		}else if(dType == 2){
			return MoneyCalc.moneyTotal(list).divide(new BigDecimal(10000)).setScale(2, BigDecimal.ROUND_HALF_UP);
		}else{
				throw new Exceptions("error!");
		}
	}
	
	/**
	 * type = 1 <=%5
	 * type = 2 >5% & <= 10%
	 * type = 3 >10%
	 * */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private List getBalanceByMoney(int type){
		String select1 = "DISTINCT l.contractid";
		String from1 = "Loangrant as l where DATE_FORMAT(l.grantdate, '%Y-%m') = DATE_FORMAT('" + this.searchDate + "', '%Y-%m') and (l.state = 2 or l.state = 3 or l.state = 0)";
		List<?> listBean = this.tContractDao.findSqlQuery(select1, from1, 0);
		List<BigDecimal> list = new ArrayList<BigDecimal>();
		if(listBean != null){
			for(int i = 0, j = listBean.size(); i < j; i++){
				Map<Object,Object> mapbean = (Map<Object, Object>)listBean.get(i);
				int loanid = (Integer)mapbean.get("contractid");
				
				String select = "new vo.MoneyDoubleBean(l.balance)";
				String from = "Loangrant as l where (l.state = 2 or l.state = 3 or l.state = 0) and l.contractid = " + loanid;
				
				List listQ = this.loaninterestDao.findQuery(select, from);
				BigDecimal listMoney = MoneyCalc.moneyTotal(listQ).divide(new BigDecimal(10000)).setScale(2, BigDecimal.ROUND_HALF_UP);
				
				if(type == 1){
					if(listMoney.compareTo(money5) == 1){
						list.add(listMoney);
					}
				}else if(type == 2){
					if((listMoney.compareTo(money5) == 0 || listMoney.compareTo(money5) == 1) && listMoney.compareTo(money10) == -1){
						list.add(listMoney);
					}
				}else if(type == 3){
					if(listMoney.compareTo(money10) == 0 || listMoney.compareTo(money10) == 1){
						list.add(listMoney);
					}
				}
				list.add(listMoney);
			}
		}
		return list;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private BigDecimal getHightMoney(){
		String select1 = "DISTINCT l.contractid";
		String from1 = "Loangrant as l where DATE_FORMAT(l.grantdate, '%Y-%m') = DATE_FORMAT('" + this.searchDate + "', '%Y-%m') and (l.state = 2 or l.state = 3 or l.state = 0)";
		List<?> listBean = this.tContractDao.findSqlQuery(select1, from1, 0);
		List<BigDecimal> list = new ArrayList<BigDecimal>();
		if(listBean != null){
			for(int i = 0, j = listBean.size(); i < j; i++){
				Map<Object,Object> mapbean = (Map<Object, Object>)listBean.get(i);
				int loanid = (Integer)mapbean.get("contractid");
				String select = "new vo.MoneyDoubleBean(l.balance)";
				String from = "Loangrant as l where (l.state = 2 or l.state = 3 or l.state = 0) and l.contractid = " + loanid;
				List listQ = this.loaninterestDao.findQuery(select, from);
				BigDecimal listMoney = MoneyCalc.moneyTotal(listQ).divide(new BigDecimal(10000)).setScale(2, BigDecimal.ROUND_HALF_UP);
				list.add(listMoney);
			}
		}
		if(list != null){
			Collections.sort(list, new Comparator() {  
		          public int compare(Object a, Object b) {  
		        	BigDecimal one = ((BigDecimal)a);
		        	BigDecimal two = ((BigDecimal)b);
		            return one.compareTo(two);   
		          } 
		      });
		}
		try {
			return list.get(list.size() - 1);
		} catch (Exception e) {
			return new BigDecimal(0.00);
		}
	}
	

	public TContractDao gettContractDao() {
		return tContractDao;
	}

	public void settContractDao(TContractDao tContractDao) {
		this.tContractDao = tContractDao;
	}

	public String getSearchDate() {
		return searchDate;
	}

	public void setSearchDate(String searchDate) {
		this.searchDate = searchDate;
	}

	@Override
	public AppInfo getInfor(String object) throws Exceptions {
		try {
			return this.appInfoDAO.findOne("name", object);
		} catch (Exception e) {
			return null;
		}
	}

	public AppInfoDAO getAppInfoDAO() {
		return appInfoDAO;
	}

	public void setAppInfoDAO(AppInfoDAO appInfoDAO) {
		this.appInfoDAO = appInfoDAO;
	}

	public LoangrantDao getLoangrantDao() {
		return loangrantDao;
	}

	public void setLoangrantDao(LoangrantDao loangrantDao) {
		this.loangrantDao = loangrantDao;
	}

	public LoangrantrecordDao getLoangrantrecordDao() {
		return loangrantrecordDao;
	}

	public void setLoangrantrecordDao(LoangrantrecordDao loangrantrecordDao) {
		this.loangrantrecordDao = loangrantrecordDao;
	}

	public LoaninterestDao getLoaninterestDao() {
		return loaninterestDao;
	}

	public void setLoaninterestDao(LoaninterestDao loaninterestDao) {
		this.loaninterestDao = loaninterestDao;
	}

	public LoaninterestrecordDao getLoaninterestrecordDao() {
		return loaninterestrecordDao;
	}

	public void setLoaninterestrecordDao(LoaninterestrecordDao loaninterestrecordDao) {
		this.loaninterestrecordDao = loaninterestrecordDao;
	}

	public AppFinanceDao getAppFinanceDao() {
		return appFinanceDao;
	}

	public void setAppFinanceDao(AppFinanceDao appFinanceDao) {
		this.appFinanceDao = appFinanceDao;
	}

}
